package com.bst.bsj.bops.web.biz.impl;

import java.util.List;
import java.util.Map;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.bst.bsj.bops.web.biz.DistrictService;
import com.bst.bsj.common.dal.daointerface.DistrictDAO;
import com.bst.bsj.common.dal.dataobject.Area;
import com.homewaimai.common.lang.Paginator;
import com.homewaimai.common.lang.Result;
import com.homewaimai.common.lang.ResultSupport;

@Service("districtService")
public class DistrictServiceImpl implements DistrictService {

  private static final Log logger = LogFactory.getLog(DistrictServiceImpl.class);

  @Autowired
  private DistrictDAO districtDAO;

  @Override
  public Result query(Map paramMap) {
    Result result = new ResultSupport(false);

    Long offset = (Long) paramMap.get("offset");
    Long length = (Long) paramMap.get("length");
    Integer page = (Integer) paramMap.get("page");
    Long total = districtDAO.getCounts(paramMap);
    List<Area> areas = districtDAO.selectAll(paramMap, offset, length);

    // 创建一个分页器(leng.intValue：每页多少条)
    Paginator paginator = new Paginator(length.intValue());
    // 设置总项数
    paginator.setItems(total == null ? 0 : total.intValue());
    // 设置当前页
    paginator.setPage(page);
    result.getModels().put("paginator", paginator);
    result.getModels().put("areas", areas);

    result.setSuccess(true);
    return result;
  }

  @Override
  public Area getArea(Long id) {
    return districtDAO.getArea(id);
  }

  @Override
  public int update(Area area) {
    int lid = 0;
    try {

      lid = districtDAO.update(area);
    } catch (Exception e) {
      logger.error("DistrictServiceImpl.update(banner) error!", e);
    }
    return lid;
  }

  @Override
  public void insert(Area area) {
    Long lid = 0L;
    try {
      lid = districtDAO.insert(area);
      Area a = new Area();
      a.setId(lid);
      a.setNumber(lid + "");
      districtDAO.update(a);
    } catch (Exception e) {
      logger.error("DistrictServiceImpl.insert(banner) error!", e);
    }
  }

  @Override
  public int delete(Long id) {
    int lid = 0;
    try {
      lid = districtDAO.delete(id);
    } catch (Exception e) {
      logger.error("DistrictServiceImpl.delete(banner) error!", e);
    }
    return lid;
  }

  @Override
  public List<Area> getAreas(String type) {
    return districtDAO.getAreas(type);
  }

  @Override
  public Area getAreaByCity(Map paramMap) {
    return districtDAO.getAreaByCity(paramMap);
  }

  @Override
  public List<Area> getAreasByParent(Map<String,Object> paramMap) {
    return districtDAO.getAreasByParent(paramMap);
  }

}
